# -*- Python -*-
#
# Copyright 2008 Google Inc. All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


"""Builds the Google Mock (gmock) lib.

You should be able to call this file from more or less any SConscript
file.

You can optionally set a variable on the construction environment to
have the unit test executables copied to your output directory.  The
variable should be env['EXE_OUTPUT'].

Another optional variable is env['LIB_OUTPUT'].  If set, the generated
libraries are copied to the folder indicated by the variable.

If you place the Google Mock sources within your own project's source
directory, you should be able to call this SConscript file simply as
follows:

# -- cut here --
# Build gmock library; first tell it where to copy executables.
env['EXE_OUTPUT'] = '#/mybuilddir/mybuildmode'  # example, optional
env['LIB_OUTPUT'] = '#/mybuilddir/mybuildmode/lib'
env.SConscript('whateverpath/gmock/scons/SConscript')
# -- cut here --

If on the other hand you place the Google Mock sources in a directory
outside of your project's source tree, you would use a snippet similar
to the following:

# -- cut here --

# The following assumes that $BUILD_DIR refers to the root of the
# directory for your current build mode, e.g. "#/mybuilddir/mybuildmode"

# Build gmock library; as it is outside of our source root, we need to
# tell SCons that the directory it will refer to as
# e.g. $BUILD_DIR/gmock is actually on disk in original form as
# ../../gmock (relative to your project root directory).  Recall that
# SCons by default copies all source files into the build directory
# before building.
gmock_dir = env.Dir('$BUILD_DIR/gmock')

# Modify this part to point to Google Mock relative to the current
# SConscript or SConstruct file's directory.  The ../.. path would
# be different per project, to locate the base directory for Google Mock.
gmock_dir.addRepository(env.Dir('../../gmock'))

# Tell the Google Mock SCons file where to copy executables.
env['EXE_OUTPUT'] = '$BUILD_DIR'  # example, optional

# Call the Google Mock SConscript to build gmock.lib and unit tests.  The
# location of the library should end up as
# '$BUILD_DIR/gmock/scons/gmock.lib'
env.SConscript(env.File('scons/SConscript', gmock_dir))

# -- cut here --
"""


__author__ = 'joi@google.com (Joi Sigurdsson)'


import os

############################################################
# Environments for building the targets, sorted by name.

Import('env', 'gtest_exports')

GTEST_DIR = env['GTEST_DIR']

GtestObject = gtest_exports['GtestObject']
GtestBinary = gtest_exports['GtestBinary']
GtestTest = gtest_exports['GtestTest']

gtest_common_exports = SConscript(GTEST_DIR + '/scons/SConscript.common')
EnvCreator = gtest_common_exports['EnvCreator']

env = env.Clone()
if env['PLATFORM'] == 'win32':
  env.Append(CCFLAGS=[
      '-wd4127',  # Disables warning "conditional expression is constant",
                  # triggered by VC 8.0's own STL header <list>.
      ])

# Note: The relative paths in SConscript files are relative to the location
# of the SConscript file itself. To make a path relative to the location of
# the main SConstruct file, prepend the path with the # sign.
#
# Include paths to gtest headers are relative to either the gmock
# directory or the 'include' subdirectory of it, and this SConscript
# file is one directory deeper than the gmock directory.
env.Prepend(CPPPATH = ['..', '../include', GTEST_DIR + '/include'])

env_use_own_tuple = EnvCreator.Create(env, EnvCreator.UseOwnTuple)
env_with_exceptions = EnvCreator.Create(env, EnvCreator.WithExceptions)
env_without_rtti = EnvCreator.Create(env, EnvCreator.NoRtti)

############################################################
# Helpers for creating build targets.

def GmockStaticLibraries(build_env):
  """Builds static libraries for gmock and gmock_main in build_env.

  Args:
    build_env: An environment in which to build libraries.

  Returns:
    A pair (gmock_library, gmock_main_library) built in the build_env
    environment.
  """

  gmock_object = GtestObject(build_env, '../src/gmock-all.cc')
  gmock_main_object = GtestObject(build_env, '../src/gmock_main.cc')

  return (build_env.StaticLibrary(target='gmock' + build_env['OBJ_SUFFIX'],
                                  source=[gmock_object]),
          build_env.StaticLibrary(target='gmock_main' + build_env['OBJ_SUFFIX'],
                                  source=[gmock_object, gmock_main_object]))


############################################################
# Object and library targets.

gtest = gtest_exports['gtest']
gtest_ex = gtest_exports['gtest_ex']
gtest_no_rtti = gtest_exports['gtest_no_rtti']
gtest_use_own_tuple = gtest_exports['gtest_use_own_tuple']

# gmock.lib to be used by most apps (if you have your own main function).
# gmock_main.lib can be used if you just want a basic main function; it is
# also used by some tests for Google Test itself.
gmock, gmock_main = GmockStaticLibraries(env)
gmock_ex, gmock_main_ex = GmockStaticLibraries(env_with_exceptions)
gmock_no_rtti, gmock_main_no_rtti = GmockStaticLibraries(env_without_rtti)
gmock_use_own_tuple, gmock_main_use_own_tuple = GmockStaticLibraries(
    env_use_own_tuple)

# Install the libraries if needed.
if 'LIB_OUTPUT' in env.Dictionary():
  env.Install('$LIB_OUTPUT', source=[gmock, gmock_main,
                                     gmock_ex, gmock_main_ex,
                                     gmock_no_rtti, gmock_main_no_rtti,
                                     gmock_use_own_tuple,
                                     gmock_main_use_own_tuple])

#############################################################
# Test targets using the standard environment.
GtestTest(env, 'gmock-actions_test', [gtest, gmock_main])
GtestTest(env, 'gmock-cardinalities_test', [gtest, gmock_main])
GtestTest(env, 'gmock-generated-actions_test', [gtest, gmock_main])
GtestTest(env, 'gmock-generated-function-mockers_test', [gtest, gmock_main])
GtestTest(env, 'gmock-generated-internal-utils_test', [gtest, gmock_main])
GtestTest(env, 'gmock-generated-matchers_test', [gtest, gmock_main])
GtestTest(env, 'gmock-internal-utils_test', [gtest, gmock_main])
GtestTest(env, 'gmock-matchers_test', [gtest, gmock_main])
GtestTest(env, 'gmock-more-actions_test', [gtest, gmock_main])
GtestTest(env, 'gmock-nice-strict_test', [gtest, gmock_main])
GtestTest(env, 'gmock-port_test', [gtest, gmock_main])
GtestTest(env, 'gmock-printers_test', [gtest, gmock_main])
GtestTest(env, 'gmock-spec-builders_test', [gtest, gmock_main])
GtestTest(env, 'gmock_leak_test_', [gtest, gmock_main])
GtestTest(env, 'gmock_link_test', [gtest, gmock_main],
          ['../test/gmock_link2_test.cc'])
GtestTest(env, 'gmock_output_test_', [gtest, gmock])
#GtestTest(env, 'gmock_stress_test', [gtest, gmock])
GtestTest(env, 'gmock_test', [gtest, gmock_main])
# gmock_all_test is commented to save time building and running tests.
# Uncomment if necessary.
#GtestTest(env, 'gmock_all_test', [gtest, gmock_main])

############################################################
# Tests targets using custom environments.
GtestBinary(env_with_exceptions,
            'gmock-more-actions-ex_test',
            [gtest_ex, gmock_main_ex],
            ['../test/gmock-more-actions_test.cc'])

GtestBinary(env_without_rtti,
            'gmock_no_rtti_test',
            [gtest_no_rtti, gmock_main_no_rtti],
            ['../test/gmock-spec-builders_test.cc'])

GtestBinary(env_use_own_tuple,
            'gmock_use_own_tuple_test',
            [gtest_use_own_tuple, gmock_main_use_own_tuple],
            ['../test/gmock-spec-builders_test.cc'])
